/**     
 * @Title: RabbitMQConfiguration.java   
 * @Package me.mq.config   
 * @Description: TODO
 * @author weiwei 
 * @date 2018年5月30日 上午9:24:05   
 * @version V1.0     
 */
package me.mq.config;

import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName: RabbitMQConfiguration
 * @Description: TODO
 * @author weiwei
 * @date 2018年5月30日 上午9:24:05
 * 
 */
@Configuration
public class RabbitMQConfiguration {

	public final static String QUEUE_NAME = "spring-boot-queue";

	public final static String EXCHANGE_NAME = "spring-boot-exchange";

	public final static String ROUTING_KEY = "spring-boot-key";

	// 创建队列
	@Bean
	public Queue queue() {
		return new Queue(QUEUE_NAME);
	}

	// 创建一个 topic 类型的交换器
	@Bean
	public TopicExchange exchange() {
		return new TopicExchange(EXCHANGE_NAME);
	}

	// 使用路由键（routingKey）把队列（Queue）绑定到交换器（Exchange）
	@Bean
	public Binding binding(Queue queue, TopicExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
	}
	
	@Bean
	public ConnectionFactory connectionFactory(){
		ConnectionFactory connectionFactory = new CachingConnectionFactory("127.0.0.1", 5672);
        return connectionFactory;
	}
	
	@Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);
    }
	
	@Bean
	public AmqpAdmin amqpAdmin(){
		return new RabbitAdmin(connectionFactory());
	}
}
